﻿using System.Reflection;

namespace ForguncyPluginPackageTool
{
    public class PluginItem
    {
        public PluginItem()
        {
            Assemblies = new List<Assembly>();
        }
        public List<string> assembly { get; set; }

        public List<Assembly> Assemblies { get; set; }

        public List<string> javascript { get; set; }
        public List<string> css { get; set; }
        public bool bundleJavaScript { get; set; }
        public bool bundleCSS { get; set; }

        public string image { get; set; }

        #region Description
        public string description { get; set; }
        public string description_ja { get; set; }
        public string description_cn { get; set; }
        public string description_kr { get; set; }

        #endregion

        #region Name
        public string name { get; set; }
        public string name_cn { get; set; }
        public string name_ja { get; set; }
        public string name_kr { get; set; }

        #endregion
        public string pluginType { get; set; }
        public string configFilePath { get; set; }
        public string relativePath { get; set; }
        public string guid { get; set; }
        public string version { get; set; }
        public string dependenceVersion { get; set; }

        public FrontEndLocalization frontEndLocalization { get; set; }

        public static int CompareVersion(PluginItem item1, PluginItem item2)
        {
            var version1 = item1?.version;
            var version2 = item2?.version;

            if (string.IsNullOrEmpty(version1) && string.IsNullOrEmpty(version2))
            {
                return 0;
            }
            else if (string.IsNullOrEmpty(version1))
            {
                return -1;
            }
            else if (string.IsNullOrEmpty(version2))
            {
                return 1;
            }
            else
            {
                return new Version(version1).CompareTo(new Version(version2));
            }
        }

        public static bool IsSameDependenceVersion(PluginItem item1, PluginItem item2)
        {
            if (item1.dependenceVersion == null && item2.dependenceVersion == null)
            //V3
            {
                return true;
            }
            else if (item1.dependenceVersion == null || item2.dependenceVersion == null)
            {
                return false;
            }
            else
            {
                return new Version(item1.dependenceVersion) == new Version(item2.dependenceVersion);
            }
        }

        public override bool Equals(object obj)
        {
            if (obj is PluginItem pluginItem)
            {
                return pluginItem.guid == guid;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return guid?.GetHashCode() ?? 0;
        }
    }

    public class FrontEndLocalization
    {
        public ExtractFromResourceFile extractFromResourceFile { get; set; }
    }

    public class ExtractFromResourceFile
    {
        public bool enable { get; set; }

        public string outputDir { get; set; }
    }
}
